<?php

namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Request;
class Login extends Controller{
    /****
     * Class register          	账号密码登录页
     * @$mobile[int]    		手机号
     * @$password[int]        	密码
     * @$mobile_id[array]   	查询
     * @$code [int]            	密码盐
     * @date                	2020/05/14 
     * @package application\index\controller\Login
    */
    public function login(){
       $mobile = input('mobile');
       $password = input('password');
       $mobile_id = Db::name('user')->where(['mobile'=>$mobile])->find();
       if($mobile_id){
          if($mobile_id['password'] != md5($mobile_id['salt'].md5($password))){
            $arr = [
             'state'     => 2,
             'message'   => "密码错误！"
            ];
            return json($arr);
          }
       		$arr = [
		       'state'     => 1,
           'user_id'   =>$mobile_id['id'],
		       'message'   => "登录成功！"
	       	];
       }else{
       		$arr = [
		       'state'     => 0,
		       'message'   => "未注册请先注册！"
	       	];
       }
       return json($arr);
    }

    /****
     * Class register          	注册页
     * @$mobile[int]    		    手机号
     * @$password[int]        	密码
     * @$user_register[array]   查询
     * @$code [int]            	密码盐
     * @$user_name[int]			    生成用户名（'ZTXY'.$user_name）
     * @$user_add[arry]			    数据添加
     * @date                	  2020/05/14 
     * @package application\index\controller\Login
    */
    public function register()
    { 
       $mobile = input('mobile');
       $password = input('password');
       $user_register = Db::name('user')->where(['mobile'=>$mobile])->find();
       if($user_register){
  		   $arr = [
  		       'state'     => 0,
  		       'message'   => "手机号已注册！"
  	       ];
           return json($arr);
       }
       $pool='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	     $word_length=6;
  	   $code = '';
       for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < $word_length; $i++){
       		$code .= $pool[mt_rand(0, $mt_rand_max)];
       }
       $user_name = ''; 
        for($i=0;$i<8;$i++){
            $randnum = rand(0,9);    
            $user_name .= $randnum; 
        }
      $str = "";
      $length = 3;
      $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
      $max = strlen($strPol)-1;
      for($i=0;$i<$length;$i++){
          $str.=$strPol[rand(0,$max)];
       }
      $length_number=6;
      $str_number = "";
      $strPol = "0123456789";
      $max = strlen($strPol)-1;
      for($i=0;$i<$length_number;$i++){
          $str.=$strPol[rand(0,$max)];
       }
	   $request = Request::instance();
       $data = [
       		'mobile'	  =>$mobile,
       		'password'	  =>md5($code.md5($password)),
       		'salt'		  =>$code,
       		'nickname'	  =>'ZTXY'.$user_name,
          'username'      => $str.$length_number,
       		'jointime'	  =>time(),
       		'createtime'  =>time(),
			'loginip'	  =>$request->ip(),
			'status'	  =>'normal'
       ];
       $user_add = Db::name('user')->insert($data);
       if($user_add){
       		$arr = [
		       'state'     => 1,
		       'message'   => "注册成功！"
	       	];
       }else{
       		$arr = [
		       'state'     => 2,
		       'message'   => "注册失败！"
	       	];

       }
       return json($arr);
    }

}
